/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.Random;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Monster
 */
public class NamDocMovement extends SpriteRandomMovement {
    
    public NamDocMovement(Sprite sprite) {
        super(sprite);
    }

    public void RandomDirection(int lastDirection) {
        Random random = new Random();
        boolean flag = true;
        do {
            int result = random.nextInt(6)+1;   
            System.out.println(result);
            switch (result) {
                case GameCanvas.LEFT: {
                    if (lastDirection != result) {
                        this.direction = result;
                        flag=false;
                    }
                    break;
                }
                case GameCanvas.RIGHT:
                    if (lastDirection != result) {
                        this.direction = result;
                        flag=false;
                    }
                    break;
                case GameCanvas.DOWN:
                    if (lastDirection != result) {
                        this.direction = result;
                        flag=false;
                    }
                    break;
                case GameCanvas.UP:
                    if (lastDirection != result) {
                        this.direction = result;
                        flag=false;
                    }
                    break;
            }
        } while (flag);
    }

    public void run() {
        while (!this.interrupted) {
            if (this.direction == GameCanvas.DOWN) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.downSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(0, getSpeed());
                if (VaChamVoiVatCanTinh() || VaChamVatCanDong()) {
                    this.sprite.move(0, -getSpeed());
                    RandomDirection(direction);
                    continue;
                }
            } else if (this.direction == GameCanvas.UP) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.upSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(0, -getSpeed());
                if (VaChamVoiVatCanTinh() || VaChamVatCanDong()) {
                    this.sprite.move(0, getSpeed());
                    RandomDirection(direction);
                    continue;
                }
            } else if (this.direction == GameCanvas.LEFT) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.leftSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(-getSpeed(), 0);
                if (VaChamVoiVatCanTinh() || VaChamVatCanDong()) {
                    this.sprite.move(getSpeed(), 0);
                    RandomDirection(direction);
                    continue;
                }
            } else if (this.direction == GameCanvas.RIGHT) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.rightSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(getSpeed(), 0);
                if (VaChamVoiVatCanTinh() || VaChamVatCanDong()) {
                    this.sprite.move(-getSpeed(), 0);
                    RandomDirection(direction);
                    continue;
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
